/*
 * File LocalUser.java 
 */

package cs.entity;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * Contains the resources specific to a session. <p>
 * Package  cs.entity
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 */
public class LocalUser extends User {
	
	/**
	 * list of the broadcast address of all the network interfaces
	 */
	private ArrayList<InetAddress> broadcastAddrList;
	
	/**
	 * list of the broadcast address of all the network interfaces
	 */
	private ArrayList<InetAddress> ipAddrList;
	
	/**
	 * Constructor
	 * @param name the user name
	 */
	public LocalUser(String username){
		super(username, null);
		broadcastAddrList= new ArrayList<InetAddress>();
		ipAddrList= new ArrayList<InetAddress>();
		
		findAddr();
		System.out.println(ipAddrList);	
		
	}

	
	/**
	 * get broadcast addresses 
	 * @return 
	 */
	public ArrayList <InetAddress> getBroadcastAddr() {
		return broadcastAddrList;
	}
	
	/**
	 * find the broadcast address of all the network interfaces
	 * @return the broadcast addresses list
	 */
	private void findAddr(){
		//broadcast address list
		//all the network interfaces of the machine
		Enumeration<NetworkInterface> interfaces;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
			while (interfaces.hasMoreElements()) {
				  NetworkInterface networkInterface = interfaces.nextElement();
				// Don't want to broadcast to the loopback interface
				  if (!networkInterface.isLoopback()) 
						  for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
							    InetAddress broadcast = interfaceAddress.getBroadcast();
							    if (broadcast !=null){
							    	broadcastAddrList.add(broadcast);
							    	ipAddrList.add(interfaceAddress.getAddress());
							    }
						  }
				}
		} catch (SocketException e) {
			e.printStackTrace();
		}
			
		
	}

	/**
	 * @return ipAddrList complete list of IP adress of local user
	 */
	public ArrayList<InetAddress> getIpAddrList() {
		return ipAddrList;
	}
	
}